package com.core.website.service.impl;

import com.core.website.entity.IpEntity;
import com.core.website.mapper.IpMapper;
import com.core.website.service.IpService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class IpServiceImpl implements IpService {
    @Autowired
    private IpMapper ipMapper;

    @Override
    public void addIp(IpEntity ipEntity) {
        IpEntity has=ipMapper.queryByColumnAndIp(ipEntity.getColumnId(),ipEntity.getIp());
        if(null==has|| StringUtils.isEmpty(has.getId())){
            String id= UUID.randomUUID().toString().toUpperCase().replace("-", "");
            ipMapper.add(id,ipEntity.getColumnId(),ipEntity.getIp());
        }
    }
    @Override
    public void update(IpEntity ipEntity) {
        ipMapper.update(ipEntity.getId(),ipEntity.getColumnId(),ipEntity.getIp());
    }

    @Override
    public List<IpEntity> queryPage(String columnId,String ip) {
        return ipMapper.queryPage(columnId,ip);
    }

    @Override
    public void delete(List<String> ipList) {
        if(null!=ipList&&ipList.size()>0){
            ipMapper.delete(ipList);
        }
    }
    @Override
    public boolean queryIsFilterColumn(String ipAddress, String columnId) {
        // 把ip地址通过点进行拆分
        String[] split1 = ipAddress.split("\\.");
        List<String> list=new ArrayList<>();
        list.add(split1[0]);
        list.add(split1[1]);
        list.add(split1[2]);
        String start = StringUtils.join(list,"."); // ip号段 127.0.0
        String end = split1[3];  // ip 地址
        // 查询该栏目下 是否存在白名单
        List<IpEntity>  ipEntityList = ipMapper.selectIpListByColumn(columnId);
        // 验证你的号段是否是这个号段的值
        if(ipEntityList.size()==0){
            return true; // 无访问限制
        }
        // 存在值 就得验证是否对你的ip地址进行放行
        for(IpEntity ipEntity:ipEntityList){
            String accessIp = ipEntity.getIp(); // 白名单ip  192.168.2.*   192.168.1.1-192.168.1.255   192.168.1.200
            if(StringUtils.isNotEmpty(accessIp)){
                if(ipEntity.getIp().contains("*")){
                    if(accessIp.startsWith(start)){
                        return true; // 白名单
                    }
                }else if(accessIp.contains("-")){
                    if(accessIp.startsWith(start)){
                        String[] split = accessIp.split("-");
                        String s=split[0].split("\\.")[3];
                        String n=split[1].split("\\.")[3];
                        if(Integer.parseInt(s)<=Integer.parseInt(end)&&Integer.parseInt(end)<=Integer.parseInt(n)){
                           return true;// 白名单
                        }
                    }
                }else{
                    if(ipAddress.equals(accessIp)){
                        return true;// 白名单
                    }
                }

            }
        }
        return false;
    }
    @Override
    public Set<String> queryFilterColumn(String ipAddress) {
        String[] split1 = ipAddress.split("\\.");
        List<String> list=new ArrayList<>();
        list.add(split1[0]);
        list.add(split1[1]);
        list.add(split1[2]);

        String start = StringUtils.join(list,".");
        String end = split1[3];
        List<IpEntity> ipEntities=ipMapper.queryAll();
        Set<String> columnIdSet=new HashSet<>();
        if(ipEntities.size()>0){
            Map<String, List<IpEntity>> collect = ipEntities.stream().collect(Collectors.groupingBy(x -> x.getColumnId()));
            for(String columnId:collect.keySet()){
                List<IpEntity> ipEntities1 = collect.get(columnId);
                boolean filter=false;
                for(IpEntity ipEntity:ipEntities1){
                    if(StringUtils.isNotEmpty(ipEntity.getIp())){
                        if(ipEntity.getIp().contains("*")){
                            if(ipEntity.getIp().startsWith(start)){
                                filter=true;
                                break;
                            }
                        }else if(ipEntity.getIp().contains("-")){
                            if(ipEntity.getIp().startsWith(start)){
                                String[] split = ipEntity.getIp().split("-");
                                String s=split[0].split("\\.")[3];
                                String n=split[1].split("\\.")[3];
                                if(Integer.parseInt(s)<=Integer.parseInt(end)&&Integer.parseInt(end)<=Integer.parseInt(n)){
                                    filter=true;
                                    break;
                                }
                            }
                        }else{
                            if(ipAddress.equals(ipEntity.getIp())){
                                filter=true;
                                break;
                            }
                        }
                    }
                }
                if(!filter){
                    columnIdSet.add(columnId);
                }
            }

        }

        return columnIdSet;
    }

    @Override
    public void statisticalIpSetting(List<String> ipList) {
        ipMapper.deleteStatisticalIpSetting();
        ipMapper.statisticalIpSetting(ipList);
    }

    @Override
    public List<String> statisticalIpSearch() {
        return ipMapper.statisticalIpSearch();
    }

    @Override
    public int statisticalIpTotal() {
        return ipMapper.statisticalIpTotal();
    }
    @Override
    public void addStatisticalIpNum(String ipAddress) {
        String[] split1 = ipAddress.split("\\.");
        List<String> list=new ArrayList<>();
        list.add(split1[0]);
        list.add(split1[1]);
        list.add(split1[2]);

        String start = StringUtils.join(list,".");
        String end = split1[3];
        List<String> ipList=ipMapper.statisticalIpSearch();
        if(null!=ipList&&ipList.size()>0){
            for(String ip:ipList){
                if(ip.contains("*")){
                    if(ip.startsWith(start)){
                        ipMapper.addStatisticalIpTotal();
                        return;
                    }
                }else if(ip.contains("-")){
                    if(ip.startsWith(start)){
                        String[] split = ip.split("-");
                        String s=split[0].split("\\.")[3];
                        String n=split[1].split("\\.")[3];
                        if(Integer.parseInt(s)<=Integer.parseInt(end)&&Integer.parseInt(end)<=Integer.parseInt(n)){
                            ipMapper.addStatisticalIpTotal();
                            return;
                        }
                    }
                }else{
                    if(ipAddress.equals(ip)){
                        ipMapper.addStatisticalIpTotal();
                        return;
                    }
                }
            }
        }
    }

    @Override
    public void first(String ip) {
        ipMapper.first(ip);
    }

    @Override
    public int checkFirst(String ip) {
        return ipMapper.checkFirst(ip);
    }
    @Override
    public String getFirstSwich() {
        return ipMapper.getFirstSwich();
    }
    @Override
    public int updateFirstSwich(String swich) {
        return ipMapper.updateFirstSwich(swich);
    }
}
